﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using xTeam.Hengle.DataAccess;
using xTeam.HengLe.Enums;

namespace xTeam.HengLe.Controller
{
    public class BaseController : System.Web.Mvc.Controller
    {
        protected string _connectionString;
        protected static ClassifyDataAccess _classifyDataAccess;
        protected static ProductDataAccess _productDataAccess;
        protected const string _imageRoot = "/ProductImages";
        protected Language _lang;

        public BaseController()
        {
            _connectionString = ConfigurationManager.ConnectionStrings["Data"].ConnectionString;
            _classifyDataAccess = new ClassifyDataAccess(_connectionString);
            _productDataAccess = new ProductDataAccess(_connectionString);
        }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            ViewData["HengLeMenu"] = _classifyDataAccess.GetHengLeMenu();
            _lang = (Language)Enum.Parse(typeof(Language), RouteData.Values["Language"].ToString().ToUpper());
        }
        /// <summary>
        /// 创建分页匿名对象
        /// </summary>
        /// <param name="HrefFormat">Url 格式字符串，
        /// 注意 UrlHelper 会将 {0} 转义，链接到字符串需要占用格式化字符串 {0} 占位符</param>
        /// <param name="pageSize">总页数</param>
        /// <param name="pageIndex">当前页码</param>
        /// <returns>分页匿名对象</returns>
        protected virtual object CreatePager(string HrefFormat, int pageCount, int pageIndex)
        {
            return new
            {
                PageIndex = pageIndex,
                PageCount = pageCount,
                HrefFormat = Server.UrlDecode(HrefFormat)
            };
        }
    }
}
